//1. 导入要到的模块（插件）
const {src,dest,watch} = require('gulp');
const htmlmin = require('gulp-htmlmin');
const sass = require('gulp-sass')(require('sass'));
const cssnano = require('gulp-cssnano');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const imagemin = require('gulp-imagemin');
//2. 发布任务
//拷贝首页
let fnCopyIndex = () => {
    return src('./src/index.html')
    .pipe(dest('./dist'));
}
//拷贝lib
let fnCopyLib = () => {
    return src('./src/lib/**/*')
    .pipe(dest('./dist/lib'));
}
//html
let fnHTML = () => {
   return  src('./src/html/**/*.html')
    .pipe(htmlmin())
    .pipe(dest('./dist/html'));
}
//css
let fnCSS = () => {
    return src('./src/css/**/*.scss')
    .pipe(sass())
    .pipe(cssnano())
    .pipe(rename({suffix : '.min'}))
    .pipe(dest('./dist/css'));
}
//js
let fnJS = () => {
    return src('./src/js/**/*.js')
    .pipe(babel({
        presets: ['@babel/env']
    }))
    .pipe(uglify())
    .pipe(rename({suffix : '.min'}))
    .pipe(dest('./dist/js'));
}
//img
let fnImg = () => {
    return src('./src/img/*')
    .pipe(imagemin())
    .pipe(dest('./dist/img'));
}
//监听
let fnWatch = () => {
    watch('./src/index.html',fnCopyIndex);
    watch('./src/lib/**/*',fnCopyLib);
    watch('./src/html/**/*.html',fnHTML);
    watch('./src/css/**/*.scss',fnCSS);
    watch('./src/js/**/*.js',fnJS);
    watch('./src/img/**/*',fnImg);
}
//3. 导出模块
exports.cloneIndex = fnCopyIndex;
exports.cloneLib = fnCopyLib;
exports.html = fnHTML;
exports.css = fnCSS;
exports.js = fnJS;
exports.img = fnImg;
exports.default = fnWatch;